import { Component, Inject, Input, OnInit } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { ApesModalRef } from '@shared/apes/modal';
import { DOCUMENT } from '@angular/common';
import { deepClone } from '../../utils/utils';


@Component({
  selector: 'dialog-expression',
  templateUrl: './dialog-expression.component.html',
  styleUrls: ['./style/index.scss'],
})
export class DialogExpressionComponent implements OnInit {

  model: any = {};
  options: any;
  resultsData: any = [];
  textareaValue: any = '';

  @Input() conditionModel: any;
  @Input() gridData: any;

  typeData: any[] = [
    { name: '列', id: 'columns', check: false, list: [] },
    {
      name: '常量', id: 'constants', check: false,
      list: [
        'True',
        'False',
        '?',
      ],
    },
    {
      name: '运算符', id: 'operators', check: false,
      list: [
        '+', '-', '*', '/', '==', '!=', '<', '<=', '>', '>=', '&&', '|',
      ],
    },
    { name: '函数', id: 'function', check: false, list: [] },
  ];
  showList: any[] = [];


  id: string;

  isLoading: boolean = false;

  constructor(private apiService: ApiService,
              @Inject(DOCUMENT) private document: any,
              public modal: ApesModalRef) {

  }

  ngOnInit() {

  }

  destroyModal(): void {
    this.modal.destroy(this.resultsData);
  }

  clickText() {
    let obj = document.getElementById('text');
    this.getSelectedText(obj);
  }

  getSelectedText(obj) {
    let userSelection;
    if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
      // 非IE浏览器
      let startPos = obj.selectionStart;// 获取选区的开始位置
      let endPos = obj.selectionEnd;// 获取选区的结束位置
      console.log('非IE：');
      console.log('选区开始点：' + startPos + '，选区结束点：' + endPos);

      userSelection = obj.value.substring(startPos, endPos);
    }
    // else if (document.selection) {
    // // IE浏览器
    // console.log('IE：');
    // userSelection = document.selection.createRange().text;
    // }
    console.log(userSelection);

  }

  selectTypeData(data, typeData) {
    let show = [];
    let list = data.list;
    typeData.forEach((td) => {
      td.check = td.id == data.id;
    });
    if (list.length != 0) {
      show = deepClone(list);
    }
    this.showList = show;
  }

}
